"""
# -*- coding: utf-8 -*-
Time    : 2025/7/28 下午3:17
Author  : Micah
File    : test_create_tag.py
Function:
"""
import requests
import pytest
import allure


@allure.feature('获得token')
@allure.step('获取所有接口的入门证书token')
def access_token():
    url = 'https://api.weixin.qq.com/cgi-bin/token?'\
          'grant_type=client_credential&appid=wx7002cc0a80&secret=903b6b342003a5a9'

    # 发送get请求
    req = requests.get(url)
    print(req.text)
    # 断言响应状态码为200，协议层断言
    assert 200 == req.status_code
    req_json = req.json()
    # 断言返回具体业务信息，数据业务层断言
    assert 7200 == req_json['expires_in']
    # 断言请求响应总时间小于3s，性能断言
    assert req.elapsed.total_seconds() < 3

    return req_json['access_token']


# 正确地创建标签用例1
@allure.feature("正确地创建标签")
@allure.story("全正确地创建")
@allure.testcase("用例1")
def test_create_tag():
    url = 'https://api.weixin.qq.com/cgi-bin/tags/create?access_token=' + access_token()
    allure.attach('数据准备')
    data = {"tag": {"name": "000"}}
    allure.attach('发送post请求')
    rep = requests.post(URL=url, json=data)
    allure.attach('断言响应状态码为200')
    assert 200 == rep.status_code
    allure.attach('断言响应返回具体业务数据')
    rep_json = rep.json()
    assert '000' == rep_json['tag']['name']


# 创建错误token用例7
@allure.feature("错误地创建标签")
@allure.story("创建错误token")
@allure.testcase("用例7")
def test_create_tag_error_token():
    url = 'https://api.weixin.qq.com/cgi-bin/tags/create?access_token=1111'
    allure.attach('发送get请求')
    rep = requests.get(url)
    allure.attach('断言响应状态码为200')
    assert 200 == rep.status_code
    # 编写断言，40001的作用可查看接口文档中全局状态码一章
    allure.attach('断言响应返回具体业务数据')
    assert 40001 == rep.json()['errcode']


if __name__ == '__main__':
    pytest.main(['-s', 'test_create_tag.py'])
